/* $Id: JCreateMapDialog.java 48 2010-01-04 01:48:58Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.GroupLayout.Alignment;

import org.crazydays.gameplan.Controller;
import org.crazydays.gameplan.map.GameMap;
import org.crazydays.gameplan.map.GridType;
import org.crazydays.gameplan.map.IGameMap;
import org.crazydays.i18n.Messages;

/**
 * JCreateMapDialog
 */
public class JCreateMapDialog
    extends JDialog
    implements ActionListener
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: JCreateMapDialog.java 48 2010-01-04 01:48:58Z aaron.day $"
            .hashCode();

    /** messages */
    public static Messages MESSAGES = Messages.getInstance();

    /** resource key: title */
    public static String RESKEY_TITLE = "JCreateMapDialog.title";

    /** resource key: label name */
    public static String RESKEY_LABEL_NAME = "JCreateMapDialog.label.name";

    /** resource key: label map type */
    public static String RESKEY_LABEL_MAP_TYPE =
        "JCreateMapDialog.label.mapType";

    /** resource key: label height */
    public static String RESKEY_LABEL_HEIGHT = "JCreateMapDialog.label.height";

    /** resource key: label width */
    public static String RESKEY_LABEL_WIDTH = "JCreateMapDialog.label.width";

    /** resource key: create */
    public static String RESKEY_LABEL_CREATE = "JCreateMapDialog.label.create";

    /** controller */
    protected Controller controller;

    /** name label */
    protected JLabel nameLabel;

    /** name input */
    protected JTextField nameInput;

    /** type label */
    protected JLabel typeLabel;

    /** type input */
    protected JComboBox typeInput;

    /** height label */
    protected JLabel heightLabel;

    /** height input */
    protected JSpinner heightInput;

    /** width label */
    protected JLabel widthLabel;

    /** width input */
    protected JSpinner widthInput;

    /** create */
    protected JButton create;

    /**
     * JCreateMap constructor.
     * 
     * @param gamePlan Game plan
     */
    public JCreateMapDialog(Controller controller)
    {
        super((Frame) controller.getView().getParent(), MESSAGES
            .format(RESKEY_TITLE), true);
        this.controller = controller;
    }

    /**
     * Initialize the form components.
     */
    public void initialize()
    {
        initializeName();
        initializeType();
        initializeHeight();
        initializeWidth();
        initializeCreate();
        initializeLayout();
        pack();
        initializeLocation();
    }

    /**
     * Initialize location.
     */
    protected void initializeLocation()
    {
        int x =
            getParent().getX() + (getParent().getWidth() / 2)
                - (getWidth() / 2);
        int y =
            getParent().getY() + (getParent().getHeight() / 2)
                - (getHeight() / 2);

        setLocation(new Point(x, y));
    }

    /**
     * Initialize name.
     */
    protected void initializeName()
    {
        nameLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_NAME));
        nameInput = new JTextField("");
    }

    /**
     * Initialize the map type components.
     */
    protected void initializeType()
    {
        typeLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_MAP_TYPE));
        typeInput = new JComboBox(GridType.values());
        typeInput.setSelectedIndex(0);
    }

    /**
     * Initialize map height components.
     */
    protected void initializeHeight()
    {
        heightLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_HEIGHT));
        heightInput = new JSpinner(new SpinnerNumberModel(480, 200, 640000, 1));
    }

    /**
     * Initialize map width components.
     */
    protected void initializeWidth()
    {
        widthLabel = new JLabel(MESSAGES.format(RESKEY_LABEL_WIDTH));
        widthInput = new JSpinner(new SpinnerNumberModel(640, 200, 640000, 1));
    }

    /**
     * Initialize create component.
     */
    protected void initializeCreate()
    {
        create = new JButton(MESSAGES.getMessage(RESKEY_LABEL_CREATE));
        create.addActionListener(this);
        getRootPane().setDefaultButton(create);
    }

    /**
     * Initialize component layout.
     */
    protected void initializeLayout()
    {
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        // horizontal
        GroupLayout.SequentialGroup horizontal = layout.createSequentialGroup();

        horizontal.addGroup(layout.createParallelGroup()
            .addComponent(nameLabel).addComponent(typeLabel).addComponent(
                heightLabel).addComponent(widthLabel));
        horizontal.addGroup(layout.createParallelGroup(Alignment.CENTER)
            .addComponent(nameInput).addComponent(typeInput).addComponent(
                heightInput).addComponent(widthInput).addComponent(create));
        layout.setHorizontalGroup(horizontal);

        // vertical
        GroupLayout.SequentialGroup vertical = layout.createSequentialGroup();
        vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(nameLabel).addComponent(nameInput));
        vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(typeLabel).addComponent(typeInput));
        vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(heightLabel).addComponent(heightInput));
        vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(widthLabel).addComponent(widthInput));
        vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(create));
        layout.setVerticalGroup(vertical);
    }

    /**
     * Action performed.
     * 
     * @param event Event
     */
    @Override
    public void actionPerformed(ActionEvent event)
    {
        String name = nameInput.getText();
        GridType type = GridType.values()[typeInput.getSelectedIndex()];
        int height = (Integer) heightInput.getValue();
        int width = (Integer) widthInput.getValue();

        IGameMap gameMap = new GameMap(name, height, width, type, 20);

        controller.addGameMap(gameMap);

        setVisible(false);
    }
}
